package cn.edu.sjtu.se.dslab.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import cn.edu.sjtu.se.dslab.action.bean.PageBean;
import cn.edu.sjtu.se.dslab.auth.User;
import cn.edu.sjtu.se.dslab.dao.IBaseDao;
import cn.edu.sjtu.se.dslab.dao.IUserProjectDao;
import cn.edu.sjtu.se.dslab.exception.DBException;
import cn.edu.sjtu.se.dslab.project.Project;
import cn.edu.sjtu.se.dslab.project.UserProject;
import cn.edu.sjtu.se.dslab.util.Const;
@Repository("userProjectDao")
public class UserProjectDaoImpl implements IUserProjectDao {

	@Resource(name = "baseDao")
	private IBaseDao<UserProject> baseDao;
	
	@Override
	public List<UserProject> getListByProject(Project project) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer();
		// 拼接sql语句
		hql.append("from UserProject up where ");
		hql.append(" up.project.id=");
		hql.append("" + project.getId());
		return baseDao.query(hql.toString());
	}

	@Override
	public List<UserProject> getListByUser(User user) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer();
		// 拼接sql语句
		hql.append("from UserProject up where ");
		hql.append(" up.user.id=");
		hql.append("" + user.getId());
		return baseDao.query(hql.toString());
	}

	@Override
	public void save(UserProject userProject) {
		// TODO Auto-generated method stub
		baseDao.save(userProject);
	}

	@Override
	public boolean delete(UserProject userProject) {
		// TODO Auto-generated method stub
		baseDao.delete(userProject);
		return true;
	}

	@Override
	public PageBean<UserProject> getPageBeanByProject(int pageNo, Project project)
			throws DBException {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer();
		// 拼接sql语句
		hql.append("from UserProject up where ");
		hql.append(" up.project.id=");
		hql.append("" + project.getId());
		PageBean<UserProject> bean = baseDao.queryByspecialHqlForPage(pageNo,
				Const.PAGESIZE, hql.toString());
		return bean;
	}

	@Override
	public boolean update(UserProject userProject) {
		// TODO Auto-generated method stub
		baseDao.update(userProject);
		return true;
	}

	@Override
	public boolean deleteById(long id) {
		// TODO Auto-generated method stub
		baseDao.delById(UserProject.class, id);
		return true;
	}

}
